﻿using Coolzon.UI.Core.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Coolzon.UI.ViewerApp.Views
{
    public partial class RenderView
    {
        /// <summary>
        /// 视图状态
        /// </summary>
        private ViewState _viewState;

        public double XMagnifyLeft
        {
            get
            {
                return (double)GetValue(XMagnifyLeftProperty);
            }
            set
            {
                SetValue(XMagnifyLeftProperty, value);
            }
        }

        public double XMagnifyTop
        {
            get
            {
                return (double)GetValue(XMagnifyTopProperty);
            }
            set
            {
                SetValue(XMagnifyTopProperty, value);
            }
        }

        public static readonly DependencyProperty XMagnifyLeftProperty = DependencyProperty.Register("XMagnifyLeft",
            typeof(double), typeof(RenderView),
            new PropertyMetadata(default(double), XMagnifyLeftPropertyChangedCallback));
        public static readonly DependencyProperty XMagnifyTopProperty = DependencyProperty.Register("XMagnifyTop",
            typeof(double), typeof(RenderView),
            new PropertyMetadata(default(double), XMagnifyTopPropertyChangedCallback));


        private static void XMagnifyTopPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RenderView renderView = d as RenderView;
            if (renderView._magnifierVisualBrush != null)
            {
                double halfW = 800 / 2.0d;
                double halfH = 800 / 2.0d;
                Rect viewBox = (renderView._magnifierVisualBrush as VisualBrush).Viewbox;
                viewBox.X = renderView.XMagnifyLeft + halfW - (viewBox.Width / 2.0d);// -xoffset;
                viewBox.Y = renderView.XMagnifyTop + halfH - (viewBox.Height / 2.0d);// -yoffset;
                (renderView._magnifierVisualBrush as VisualBrush).Viewbox = viewBox;
                Canvas.SetLeft(renderView._ctlMagnifier, renderView.XMagnifyLeft);
                Canvas.SetTop(renderView._ctlMagnifier, renderView.XMagnifyTop);
            }
        }

        private static void XMagnifyLeftPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RenderView renderView = d as RenderView;
            if (renderView._magnifierVisualBrush != null)
            {
                double halfW = 800 / 2.0d;
                double halfH = 800 / 2.0d;
                Rect viewBox = (renderView._magnifierVisualBrush as VisualBrush).Viewbox;
                viewBox.X = renderView.XMagnifyLeft + halfW - (viewBox.Width / 2.0d);// -xoffset;
                viewBox.Y = renderView.XMagnifyTop + halfH - (viewBox.Height / 2.0d);// -yoffset;
                (renderView._magnifierVisualBrush as VisualBrush).Viewbox = viewBox;
                Canvas.SetLeft(renderView._ctlMagnifier, renderView.XMagnifyLeft);
                Canvas.SetTop(renderView._ctlMagnifier, renderView.XMagnifyTop);
            }
        }

    }
}
